<?php
/**
 * Interface/Popup to Create/Update a Patient monitoring
 * 
 * @author Xavier VILAIN
 * @package \Nawa\MedicalStudy\MedicalCenter\Patient
 * @license
 * 
 */

use Fox\Html\CheckBox;
use Fox\Generator;
use Fox\Html\TextArea;
use Nawa\MedicalStudy\MedicalCenter\Patient\Monitoring;
use Fox\Popup;
use Nawa\MedicalStudy\MedicalCenter;
use Nawa\Customer;
use Fox\Html\DropList;
use Fox\Html\Block;
use Fox\Html\Input;
use Fox\Html\Screen;

// Initialize
$xview = Xview::instance();
$monitoringid = $xview->val('monitoringid');
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$patientid = $xview->val('patientid');
$itemid = $xview->val('itemid');
$gridName = $xview->val('gridname','MC_gMonitorings_'.$patientid);
 
$keys = array('msid' => $msid,'mcid' => $mcid);
$ms = $xview->component('medicalstudies','object',$keys);
/* @var $mcPartner Nawa\MedicalStudy\MedicalCenter */
$mcPartner = $xview->component('medicalstudies','medicalcenter',$keys);
$mc = $mcPartner->medicalCenter();
$patient = $mcPartner->findPatients($msid.':'.$mcid.':'.$patientid);

if(!empty($monitoringid)) {
	$action = 'update';
	$popupLabel = Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_UPDATE);
	/* @var Nawa\MedicalStudy\MedicalCenter\Patient\Monitoring */
	$monitoring = $patient->findMonitorings($msid.':'.$mcid.':'.$patientid.':'.$monitoringid);
	$data = $monitoring->getData();
} else {
	$action = 'add';
	$popupLabel = Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_ADD);
	$data = array('monitoring_date' => time(),'author' => User::current()->contactId());
	$monitoring = new Monitoring();
}

$screen = new Screen('sMonitoring',$action,null,true,Generator::ajaxReload($gridName));
$bl = new Block('bMonitoring');
// Followup List
if(empty($itemid)) {
	$bl->add(new DropList(array('monitoring_followup','itemid'),true,false,true))
		->options($ms->followup(),'itemid',Generator::call('label'));
	
}
$bl->add(new Input('monitoring_date',true));
// Monitoring Author
$authors = Fox\Dao::finder(Customer::className())
		->follow(Customer::LINK_MEDICAL_CENTERS)
		->where('t2.msid = ? AND t2.mcid = ?',array($msid,$mcid))
		->find();
$bl->add(new DropList(array('monitoring_author','author'),true,false,true))
		->options($authors,'customerid','name1');
// Correction Asked
$bl->add(new CheckBox('corrections_asked'));
// Comments
$bl->add(new TextArea(array('monitoring_comments','comments'),false,null,200,12))
	->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_COMMENTS));
// Patient 
$bl->add(new CheckBox(array('patient_validated','validated'),false));
$screen->add($bl);
$screen->initialize($data);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			$fields['itemid'] = $itemid;
			if($monitoring->exists()) {
				$monitoring->setFields($fields);
				if($monitoring->update() ){
					Fox\Message::Success(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_UPDATED));
				} else 	{
					Fox\Message::Information(MSg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE));
				}
			} else {
				$fields['msid'] = $msid;
				$fields['mcid'] = $mcid;
				$fields['patientid'] = $patientid;
				$fields['monitored'] = 0;
				$monitoring->setFields($fields);
				$monitoring->create();
				Fox\Message::Success(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_CREATED));
			}
		} catch(Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
		echo $xview->messages(true,true);
		exit;
	}
}
$popup =  new Popup('pMonitoring',$popupLabel,$screen,600);
echo $popup->display();